package com.kdtech.analyse.news;
import com.alibaba.fastjson.JSONArray;
import com.alibaba.fastjson.JSONObject;
import com.kdtech.analyse.AnalyseNews;
import com.kdtech.utils.HtmlCleaner;

import java.io.IOException;
import java.util.Collection;
import java.util.Collections;
import java.util.HashMap;
import java.util.Set;
import java.util.Map.Entry;
import java.util.regex.Matcher;
import java.util.regex.Pattern;

import org.jsoup.Jsoup;
import org.jsoup.nodes.Document;
import org.jsoup.select.Elements;

import com.kdtech.analyse.JSoupUtils;
import com.kdtech.crawler.CrawlHTML;
import com.kdtech.entity.crawler.UrlMeta;
import com.kdtech.entity.data.NewsMeta;
import com.kdtech.utils.DateUtils;
import com.kdtech.utils.StringUtils;
import com.kdtech.utils.HtmlCleaner;
/**
 * http://www.163.cn/ 网易
 * @author allen
 */
public class N163NewsAnalyse implements AnalyseNews {

	//获取网易滚动新闻链接
	public static HashMap<String, String> getLinks(String url)
	{
		HashMap<String, String> urlMap = new HashMap<String, String>();
		String realUrl = "http://news.163.com/special/0001220O/news_json.js";
		UrlMeta meta = CrawlHTML.responseToURL(realUrl);
		if(meta != null && meta.getHtml().length() > 1000){
			String htmltxt = meta.getHtml();
			String jsontxt = StringUtils.substringBetween(htmltxt, "var data=", ";");
			JSONObject json =JSONObject.parseObject(jsontxt);
			JSONArray jsonArray = json.getJSONArray("news");
			for (int i = 0; i < jsonArray.size(); i++) {
				JSONArray detailList = jsonArray.getJSONArray(i);
				for (int j = 0; j < detailList.size(); j++) {
					JSONObject obj = detailList.getJSONObject(j);
					String u = obj.getString("l");
					String title = obj.getString("t");
					urlMap.put(u,title);
				}
			}
		}
		return urlMap;
	}

	
	public boolean isDetailPage(String url) {
		boolean bRet=false;
		String[] regex={
				"http://.*.163.com/photoview/[0-9A-Z]+/[0-9]+.html",
				"http://.*.163.com/.*[0-9]{2}/[0-9]{4}/.*/.*.html.*",
				"http://quotes.money.163.com/.*/.*_[0-9]*_[0-9]*.html",
				"http://news.163.com/air/photoview/[0-9A-Z]*/[0-9]*.html",
				};
		for (int i=0; i < regex.length; i++) {
			if (url.matches(regex[i])) {
				return true;
			}
		}
		return bRet;
	}

	
	public NewsMeta parserHtml(UrlMeta urlMeta) {
		NewsMeta news=new NewsMeta();
		if (urlMeta.getHtml() == null) {
		}
		String html=urlMeta.getHtml();
		String url=urlMeta.getUrl();
		if(!isDetailPage(url)){
		}
		news.setUrl(url);
		String title=null;
		String content=null;
		Long date=null;
		String dateStr=null;
		Document doc=Jsoup.parse(html);
		if(url.matches("http://view.163.com/.*[0-9]{2}/[0-9]{4}/.*/.*.html.*")){
			title =doc.select("div.area > div.colLM > div.brief > h3").text();
			if(title==null || title.trim().length()==0){
			}
			content=HtmlCleaner.getContentHtml(url,doc.select("div.area > div.colLM > div.feed-text"));
			String tempdate=doc.select("div.area > div.colLM > div.brief > p#ptime").text();
			date=DateUtils.matchDate(tempdate);
			news.setTitle(StringUtils.trimSpace(title));
			news.setContent(StringUtils.trimSpace(content));
			news.setDate(date);

		}else if(url.indexOf("/photoview/")!=-1){
			title=doc.select("div.headline h1").text();
			content=HtmlCleaner.getContentHtml(url,doc.select("div.overview,div.picinfo-text"));
			date=DateUtils.matchDate(doc.select("div.headline span").text());
		}else{
			title =doc.select("div.endContent > h1#h1title").text();

			if (StringUtils.isBlank(title)){
				title =doc.select("h1#h1title").text();
				if(title==null || title.trim().length()==0){
					title =doc.select("div.atc_title h1").text();
					if(title==null || title.trim().length()==0){
						title =doc.select("h1#endTitle").text();
						if(title==null || title.trim().length()==0){
							title =doc.select("h1").text();
						}
					}
				}
			}
			if (StringUtils.isBlank(title)){
				title=doc.select("div.atitle").text();
			}
			doc.select("a:containsOwn(位网友发表了看法，点击查看)").remove();
			content=HtmlCleaner.getContentHtml(url,doc.select("div.endContent > div#endText"));
			if(StringUtils.isNullOrEmpty(content)){
				content=HtmlCleaner.getContentHtml(url,doc.select("div#endText"));
				if(StringUtils.isNullOrEmpty(content)){
					content=HtmlCleaner.getContentHtml(url,doc.select("div#text"));
				}
			}
			if(StringUtils.isNullOrEmpty(content)){
				content=HtmlCleaner.getContentHtml(url,doc.select("div.content"));
			}
			if(StringUtils.isNullOrEmpty(content)){
				content=HtmlCleaner.getContentHtml(url,doc.select("div.inner_box"));
			}
			date=DateUtils.matchDate(doc.select("span.info").text());
			if (date==null){
				date=DateUtils.matchDate(doc.select("div.ep-info,div.text,#subjection").text());
			}
		}
		if(date==null){
			date=DateUtils.matchDate(doc.select("div.inner_box").text());
		}
		if(date==null){
			date=DateUtils.matchDate(doc.select("span#ptime").text());
		}
		if(date==null){
			date=JSoupUtils.matchDate(doc, "来源:");
		}

		if(date==null){
			date=DateUtils.matchDate(url);
		}
		news.setTitle(title);
		news.setContent(content);
		news.setDate(date);
		news.setDate(date);

		String author=null;
		Elements select=doc.select("div#epContentLeft div.ep-info div.left a");
		if(select!=null && select.size()!=0){
			author=select.get(0).text();
		}
		if(StringUtils.isBlank(author)){
			author=JSoupUtils.matchAuthor(doc, "来源：");
		}
		news.setAuthor(author);

		Pattern p=Pattern.compile(".*（([一二三四五六七八九十(全文)]+)?）");
        Matcher m=p.matcher(title);
        if (m.find()) {
        	String group=m.group(1);
        	title=title.replace("（"+group+"）","");
        	news.setTitle(title);
        }

		return news;
	}
	
	public NewsMeta Update(NewsMeta meta) {
		return null;
	}
	public static void main(String[] args) throws IOException {
		System.out.println(DateUtils.format(1443049566*1000l,"yyyy/MM/dd HH:mm:ss"));
		N163NewsAnalyse.getLinks("http://news.163.com/latest");
		N163NewsAnalyse a=new N163NewsAnalyse();
		String url="http://news.163.com/air/photoview/5MC30001/102092.html";
		System.out.println(a.isDetailPage(url));
		UrlMeta meta=CrawlHTML.responseToURL(url);
		NewsMeta parserHtml=a.parserHtml(meta);
		System.out.println(parserHtml);
	}


	
	public boolean isNeedUpdate(){
		return false;
	}
}
